package com.company.project.web;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.*;
import com.company.project.service.SysResAuthService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/**
* Created by CodeGenerator on 2020/04/18.
*/
@RestController
@RequestMapping("/sys/res/auth")
public class SysResAuthController {
    @Resource
    private SysResAuthService sysResAuthService;

    @PostMapping("/add")
    public Result add(SysResAuth sysResAuth) {
        sysResAuthService.save(sysResAuth);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        sysResAuthService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(SysResAuth sysResAuth) {
        sysResAuthService.update(sysResAuth);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        SysResAuth sysResAuth = sysResAuthService.findById(id);
        return ResultGenerator.genSuccessResult(sysResAuth);
    }

    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<SysResAuth> list = sysResAuthService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    /**
     * 批量添加功能权限
     * @param data 数据
     * @return
     */
    @RequestMapping("/batchAddResAuth")
    public Result batchAddResAuth(@RequestBody HashMap<String,Object> data){
        int roleId = (Integer) data.get("roleId");
        List<HashMap<String,Integer>> res = (List<HashMap<String,Integer>>)data.get("res");
        return sysResAuthService.batchAddResAuth(roleId,res);
    }

    /**
     * 批量修改功能权限
     * @param data 数据
     * @return
     */
    @RequestMapping("/batchUpdateResAuth")
    public Result batchUpdateResAuth(@RequestBody HashMap<String,Object> data){
        int roleId = Integer.parseInt(data.get("roleId").toString());
        List<HashMap<String,Integer>> res = (List<HashMap<String,Integer>>)data.get("res");
        return sysResAuthService.batchUpdateResAuth(roleId,res);
    }

    /**
     * 查询功能权限列表
     * @return
     */
    @RequestMapping("/queryResAuthList")
    public Result queryResAuthList(){
        SysUser SysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        return sysResAuthService.queryResAuthList(SysUser.getId());
    }


    /**
     * 根据角色查询功能权限(菜单)
     * @param roleId 角色id
     * @return
     */
    @RequestMapping("/queryResListTreeByRole")
    public Result queryResListTreeByRole(@RequestParam int roleId, int functionId, int type){
        return sysResAuthService.queryResListTreeByRole(roleId,functionId,type);
    }

    /**
     * 根据功能id和子功能类型查询下级功能列表
     * @param functionId
     * @param type
     * @return
     */
    @RequestMapping("/queryResChildren")
    public Result queryResChildren(int functionId,int type){
        List<SysFunctionTreeDto> list = new ArrayList<SysFunctionTreeDto>();
        list = sysResAuthService.getChildrenTree(list,null,functionId,type);
        return ResultGenerator.genSuccessResult(list);
    }

    /**
     * 根据父id查询子功能列表
     * @param pId
     * @param type
     * @param queryType 查询类型
     * @return
     */
    @RequestMapping("/queryMenuListByPid")
    public Result queryMenuListByPid(int pId,@RequestParam(defaultValue = "0") int type
            ,@RequestParam(defaultValue = "0") int queryType){
        try {
            SysUser SysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
            if (queryType == 1){
                List<SysFunction> list = sysResAuthService.selectByParentIdPlus(pId,type,SysUser.getId());
                return ResultGenerator.genSuccessResult(list);
            }else {
                List<SysFunction> list = sysResAuthService.selectByParentId(pId,type);
                return ResultGenerator.genSuccessResult(list);
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult();
        }

    }
    /**
     * 根据父id查询子功能列表
     * @param pId
     * @param type
     * @return
     */
    @RequestMapping("/queryMenuListAndChildrenByPid")
    public Result queryMenuListAndChildrenByPid(int pId,@RequestParam(defaultValue = "0") int type){
        List<SysFunctionMenuDto> list = new ArrayList<SysFunctionMenuDto>();
        try {
            Result roleAuthList = queryResAuthList();
            if (null == roleAuthList || !ResultGenerator.isSuccess(roleAuthList) || roleAuthList.getData() == null){
                return null;
            }
            List<SysFunction> functions = (List<SysFunction>)roleAuthList.getData();
            HashSet<Integer> keyMap = new HashSet<Integer>();
            for (int i=0;i<functions.size();i++){
                keyMap.add(functions.get(i).getId());
            }
            list = sysResAuthService.queryMenuListByPid(list,keyMap,pId,type);
            return ResultGenerator.genSuccessResult(list);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult();
        }

    }

    @RequestMapping("/getFunctionByUser")
    public Result getFunctionBySysUser(@RequestParam(defaultValue = "1") int type, int currentId){
        SysUser SysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        return sysResAuthService.getFunctionByUser(SysUser.getId(),type,currentId);
    }

    @RequestMapping("/getFunctionTreeByUser")
    public Result getFunctionTreeByUser(@RequestParam(defaultValue = "1") int type, int currentId){
        SysUser SysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        return sysResAuthService.getFunctionTreeByUser(SysUser.getId(),type,currentId);
    }

    @RequestMapping("/getFunctionTreeByRole")
    public Result getFunctionTreeByRole(@RequestParam int roleId, @RequestParam(defaultValue = "1") int type, Integer currentId){
        return sysResAuthService.getFunctionTreeByRole(roleId,type,currentId);
    }
}
